import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_module/base/model/constants.dart';
import 'package:flutter_module/controller/third_controller.dart';
import 'package:flutter_module/router/routers.dart';
import 'package:flutter_module/util/app_colors.dart';
import 'package:get/get.dart';

class ThirdPage extends StatefulWidget {
  @override
  _ThirdPage createState() => _ThirdPage();
}

class _ThirdPage extends State<ThirdPage> {
  final _controller = Get.find<ThirdController>();

  @override
  void initState() {
    super.initState();
    _controller.loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        extendBodyBehindAppBar: true,
        appBar: AppBar(
          toolbarHeight: 0,
          backgroundColor: Theme.of(context).scaffoldBackgroundColor, //状态栏的背景颜色
          systemOverlayStyle: SystemUiOverlayStyle.light,
        ),
        body: SafeArea(
          child: GetBuilder<ThirdController>(builder: (controller) {
            return Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Expanded(
                  flex: 2,
                  child: Container(
                    color: AppColors.color_ffffff,
                    child: ListView.builder(
                      itemCount: _controller.listInfo?.length,
                      itemBuilder: (BuildContext context, int position) {
                        return getRow(position);
                      },
                    ),
                  ),
                ),
                Expanded(
                    flex: 5,
                    child: ListView(
                      children: <Widget>[
                        Container(
                          alignment: Alignment.topLeft,
                          padding: const EdgeInsets.all(10),
                          color: AppColors.color_f9f9f9,
                          child: getChip(_controller.index),
                        ),
                      ],
                    )),
              ],
            );
            ;
          }),
        ));
  }

  Widget getRow(int i) {
    Color textColor = AppColors.color_3cceaf; //字体颜色
    return GestureDetector(
      child: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(vertical: 15, horizontal: 10),
        //Container下的color属性会与decoration下的border属性冲突，所以要用decoration下的color属性
        decoration: BoxDecoration(
          color: _controller.index == i
              ? AppColors.color_f9f9f9
              : AppColors.color_ffffff,
          border: Border(
            left: BorderSide(
              width: 5,
              color: _controller.index == i
                  ? AppColors.color_3cceaf
                  : Colors.white,
            ),
          ),
        ),
        child: Text(
          _controller.listInfo?[i].name ?? '',
          style: TextStyle(
            color: _controller.index == i ? textColor : AppColors.color_666666,
            fontWeight:
                _controller.index == i ? FontWeight.w600 : FontWeight.w400,
            fontSize: 16,
          ),
        ),
      ),
      onTap: () {
        _controller.changeIndex(i);
        textColor = AppColors.color_3cceaf;
      },
    );
  }

  Widget getChip(int i) {
    //更新对应下标数据
    _controller.updateArticles(i);
    return Wrap(
      spacing: 10.0, //widget间距
      direction: Axis.horizontal,
      alignment: WrapAlignment.start,
      children: List<Widget>.generate(
        _controller.articles?.length ?? 0,
        (int index) {
          return ActionChip(
            label: Text(
              _controller.articles![index].title ?? '',
              style: TextStyle(fontSize: 16, color: AppColors.color_666666),
            ),
            elevation: 3,
            backgroundColor: Colors.grey.shade200,
            onPressed: (){
              ///flutter页面跳转flutter页面
              BoostNavigator.instance
                  .push(RouteGet.web, arguments: {Constants.url: _controller.articles![index].link});
            },
          );
        },
      ).toList(),
    );
  }
}
